﻿@using KRM.Core
@using KRM.Core.General
@model KRM.Core.tblKRMWPost
@{
    tblUser userCurrent = ViewBag.User;
    ViewBag.Title = "View Post";
    Layout = userCurrent == null ? "~/Views/Shared/_NewsFeedNonUserLayout.cshtml" : "~/Views/Shared/_Layout.cshtml";
    KRMEntities db = ViewBag.db;
    var user = Model.User;
}

<div class="container-fluid newsfeed">
    <div class="page-header"></div>
    <div class="row-fluid">
        <div class="span@(userCurrent == null ? 12: 8)">
            <div class="postitem">
                <div class="postavata">
                    <a href="/?author=@Model.PostByUserId" title="@user.Username">
                        <img src="/Upload/Avatar/@(user.Avatar ?? "user-default.png")" onerror="this.src='/upload/avatar/user-default.png'" alt="@user.Username"/>
                    </a>
                </div>
                <div class="postsummary">
                    <p class="postauthor">
                        <a href="?author=@Model.PostByUserId" title="@user.Username" class="bold">@user.Username</a>
                        <span class="postdate">@string.Format("{0: HH:mm tt on MMMM d, yyyy}", Model.PostDate)</span>
                    </p>
                    <div class="postcontent">
                        @switch (Model.PostTypeId)
                        {
                            case 1:
                            @Html.Raw(Model.Content)
                                break;
                            case 2:
                                if (!string.IsNullOrEmpty(Model.Content))
                                {
                            @Html.Raw(Model.Content)
                                }
                                foreach (var image in db.tblKRMWPostImages.Where(a => a.PostId == Model.PostId))
                                {
                            <a href="@image.Url" target="_blank">
                                <img src="@image.Url" alt="" style="margin-top: 10px"/>
                            </a>
                                }
                                break;
                            case 3:
                            <blockquote>
                                <p>@Html.Raw(Model.Content)</p>
                                <small>@Model.Title</small>
                            </blockquote>
                                break;
                            case 4:
                            <h3>
                                <a href="/post/view/@Model.PostId" title="@Model.Title">@Model.Title</a>
                            </h3>
                            @Html.Raw(Model.Content.Replace("\r\n", "<br />"))
                                break;
                        }
                    </div>

                    <div class="postcomment commentpostdetail" id="comment">
                        <h4>Comments</h4>
                        @if (userCurrent != null)
                        {
                            <form class="margin0" method="POST" data-id="@Model.PostId" action="/post/commentpostdetail">
                                <input type="hidden" name="postid" value="@Model.PostId"/>
                                <div class="row-fluid">
                                    <textarea class="input-block-level boxcomment" rows="3" name="content"></textarea>
                                </div>
                                <div class="text-right">
                                    <input type="submit" class="btn btn-primary w100" value="Comment" />
                                </div>
                            </form>
                        }
                        <div class="listcomment listcomment@(Model.PostId)">
                            @foreach (var comment in db.tblKRMWPostComments.Where(a => a.PostId == Model.PostId).OrderByDescending(a => a.CommentId))
                            {
                                var userComment = comment.User;
                                <div class="formcomment commentitem">
                                    <div class="commentavatar">
                                        <a href="/?author=@comment.CommentByUserId" title="@userComment.Username">
                                            <img src="/Upload/Avatar/@(userComment.Avatar ?? "user-default.png")" onerror="this.src='/upload/avatar/user-default.png'" alt="@userComment.Username"/>
                                        </a>
                                    </div>
                                    <div class="commentcontent">
                                        <div>
                                            <a href="/?author=@comment.CommentByUserId" title="@userComment.Username" class="bold" style="margin-right: 8px">@userComment.Username</a>
                                            <span class="timeago">@General.TimeAgo(comment.CommentDate.To<DateTime>())</span>
                                            @if (userCurrent != null && userCurrent.UserID == comment.CommentByUserId)
                                            {
                                                <div class="deletecomment" onclick="deletecomment('@comment.CommentId', '@comment.PostId')" title="Delete comment"></div>
                                            }
                                        </div>
                                        <div class="commenttext">
                                            @Html.Raw(comment.Content.Replace("\r\n", "<br />"))
                                        </div>
                                    </div>
                                </div>
                            }
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
